<?php

namespace app\admin\controller\dashboards;

use app\admin\model\exchange\Order;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\StoreConfig;
use think\Session;

/**
 * 统计图表示例
 *
 * @icon   fa fa-charts
 * @remark 展示在FastAdmin中使用Echarts展示丰富多彩的统计图表
 */
class Agent extends Backend
{
    protected $model = null;
    protected $agent_id = -1;
    protected $store_no = -1;
    protected $admin_id = -1;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('AdminLog');
        $user_info = Session::get('admin');//获取当前登录用户信息
        $this->agent_id = $user_info['agent_id'];
        $this->store_no = $user_info['store_no'];
        $this->admin_id = $user_info['id'];
    }

    /**
     * 查看
     */
    public function index()
    {

        $lingdian = strtotime(date('Y-m-d 00:00:00'));//后面要改成根据配置来获取时间

        //获取当前订单情况
        $totle_pay_arr = [];
        $totle_num_arr = [];
        $totle_pay_wx_arr = [];
        $totle_pay_zfb_arr = [];
        $all_pay_order_tmp = Order::where('agent_id',$this->agent_id)
            ->where('is_pay',2)
            ->where('createtime','>',$lingdian)
            ->where('is_delete',0)//is_delete只在店铺中生效
            ->select();
        if($all_pay_order_tmp){
            foreach ($all_pay_order_tmp as $item){
                $totle_pay_arr[] = $item->total_pay_price;
                $totle_num_arr[] = 1;
                if($item->pay_type == 1){
                    $totle_pay_wx_arr[] = $item->total_pay_price;
                }elseif ($item->pay_type == 2){
                    $totle_pay_zfb_arr[] = $item->total_pay_price;
                }
            }
        }
        $totle_pay = array_sum($totle_pay_arr);
        $totle_pay_wx = array_sum($totle_pay_wx_arr);
        $totle_pay_zfb = array_sum($totle_pay_zfb_arr);
        $totle_num = count($totle_num_arr);

        //当前用户信息
        $today_user = 0;
        $today_user_tmp = User::where('agent_id',$this->agent_id)->where('createtime','>',$lingdian)->select();//今天注册数
        if($today_user_tmp){
            $today_user = count($today_user_tmp);
        }
        $all_user = 0;
        $all_user_tmp = User::where('agent_id',$this->agent_id)->select();//总注册数
        if($all_user_tmp){
            $all_user = count($all_user_tmp);
        }

        //获取前7天订单汇总报告
        $sen_date = date('Y-m-d',strtotime('-7 day'));
        $totle_report_pay = [];
        $totle_report_num = [];
        $all_report_order_tmp = \app\admin\model\report\Order::where('agent_id',$this->agent_id)->where('date','>',$sen_date)->select();
        if($all_report_order_tmp){
            foreach ($all_report_order_tmp as $value){
                $totle_report_pay_arr[$value->date][] = $value->pay_price;
                $totle_report_num_arr[$value->date][] = $value->pay_num;

                $totle_report_pay[$value->date] = array_sum($totle_report_pay_arr[$value->date]);
                $totle_report_num[$value->date] = array_sum($totle_report_num_arr[$value->date]);
            }
        }
        $totle_report_pay_key = '';
        $totle_report_pay_val = '';
        if($totle_report_pay){
            foreach ($totle_report_pay as $kk=>$vv){
                $totle_report_pay_key_arr[] = $kk;
                $totle_report_pay_val_arr[] = $vv;
            }
            $totle_report_pay_key = json_encode($totle_report_pay_key_arr);
            $totle_report_pay_val = json_encode($totle_report_pay_val_arr);
        }
        $totle_report_num_key = '';
        $totle_report_num_val = '';
        if($totle_report_num){
            foreach ($totle_report_num as $kk=>$vv){
                $totle_report_num_key_arr[] = $kk;
                $totle_report_num_val_arr[] = $vv;
            }
            $totle_report_num_key = json_encode($totle_report_num_key_arr);
            $totle_report_num_val = json_encode($totle_report_num_val_arr);
        }

//        var_dump($totle_report_pay);
//        var_dump($totle_report_num);die;

        $this->view->assign("total_pay", $totle_pay);
        $this->view->assign("total_num", $totle_num);
        $this->view->assign("total_pay_wx", $totle_pay_wx);
        $this->view->assign("total_pay_zfb", $totle_pay_zfb);
        $this->view->assign("today_user", $today_user);
        $this->view->assign("all_user", $all_user);
        $this->view->assign("totle_report_pay_key", $totle_report_pay_key);
        $this->view->assign("totle_report_pay_val", $totle_report_pay_val);
        $this->view->assign("totle_report_num_key", $totle_report_num_key);
        $this->view->assign("totle_report_num_val", $totle_report_num_val);

        return $this->view->fetch();
    }

    /**
     * 详情
     */
    public function detail($ids)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $this->view->assign("row", $row->toArray());
        return $this->view->fetch();
    }
}
